<template>
  <div class="container">
    <el-card>
      <div class="search">
        <el-form :inline="true">
          <el-form-item label="学科名称">
            <el-input
              v-model="query.subjectName"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="eliminate">清除</el-button>
            <el-button type="primary" @click="getSubjectlist()">搜索</el-button>
          </el-form-item>
          <el-button
            style="margin-left: 600px"
            type="success"
            size="small"
            icon="el-icon-edit"
            @click="addSubject({ subjectName: '', isFrontDisplay: 0 })"
            >新增学科</el-button
          >
        </el-form>
      </div>
      <div class="total" style="padding: 0 20px">
        <i class="el-icon-info" style="marginright: 10px"></i>
        <span>数据一共有{{ counts }}条</span>
      </div>
      <el-table :data="Subjectlist" style="width: 100%">
        <el-table-column type="index" label="序号" width="50px">
        </el-table-column>
        <el-table-column prop="subjectName" label="学科名称" width="150px">
        </el-table-column>
        <el-table-column prop="username" width="140px" label="创建者">
        </el-table-column>
        <el-table-column prop="addDate" width="160px" label="创建日期">
        </el-table-column>
        <el-table-column
          prop="isFrontDisplay"
          label="前台是否显示"
          width="140px"
        >
          <template #default="{ row }">
            {{ formState(row.isFrontDisplay) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="twoLevelDirectory"
          label="二级目录"
          width="140px"
        >
        </el-table-column>
        <el-table-column prop="tags" label="标签" width="140px">
        </el-table-column>
        <el-table-column prop="totals" label="题目数量" width="140px">
        </el-table-column>
        <el-table-column prop="script" label="操作" width="230px">
          <template #default="{ row }">
            <el-button @click="goDetail(row)" type="text" size="small">
              学科分类
            </el-button>
            <el-button @click="editDetail(row)" type="text" size="small">
              学科标签
            </el-button>
            <el-button @click="editSubject(row)" type="text" size="small">
              修改
            </el-button>
            <el-button @click="delSubject(row)" type="text" size="small">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <div class="block">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
            :current-page="query.page"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="query.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="counts"
          >
          </el-pagination>
        </div>
      </el-row>
      <!--新增弹框-->
      <el-dialog
        width="30%"
        :visible="showDialog"
        :title="dialogTitle"
        @close="closeDialog"
      >
        <el-form
          ref="formName"
          label-width="100px"
          :model="formData"
          :rules="rules"
        >
          <el-form-item label="学科名称" prop="subjectName">
            <el-input
              v-model="formData.subjectName"
              placeholder="请输入学科名称"
            />
          </el-form-item>
          <el-form-item label="是否显示">
            <el-switch
              v-model="formData.isFrontDisplay"
              active-text="显示"
              :active-value="1"
              inactive-text="不显示"
              :inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div style="text-align: right">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="confirm">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { list, add, update, remove } from '@/api/hmmm/subjects.js'
export default {
  name: 'SubjectIndex',
  data () {
    return {
      dialogTitle: '',
      Subjectlist: [],
      showDialog: false,
      counts: null,
      query: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      formData: {
        subjectName: '',
        isFrontDisplay: ''
      },
      dialogVisible: false,
      rules: {
        subjectName: [
          { required: true, message: '学科名称必填', trigger: 'blur' }
        ]
      }
    }
  },
  // 钩子函数 生命周期函数
  mounted () {
    this.getSubjectlist()
  },
  methods: {
    async getSubjectlist () {
      const res = await list(this.query)
      console.log(res)
      this.Subjectlist = res.data.items
      this.counts = res.data.counts
    },
    addSubject ({ subjectName, isFrontDisplay }) {
      this.dialogTitle = '新增学科'
      this.showDialog = true
      this.formData.subjectName = subjectName
      this.formData.isFrontDisplay = isFrontDisplay
    },
    handleCurrentChange (page) {
      console.log(page)
      this.query.page = page
      this.getSubjectlist()
    },
    handleSizeChange (page) {
      this.query.pagesize = page
      this.getSubjectlist()
    },
    // 清除搜索框
    eliminate () {
      this.query.subjectName = ''
      this.getSubjectlist()
    },
    subjectAdd () {
      this.dialogVisible = true
    },
    closeDialog () {
      this.showDialog = false
    },
    // 兜底校验
    confirm () {
      this.$refs.formName.validate(async valid => {
        if (valid) {
          if (this.formData.id) {
            await update(this.formData)
            this.$message({
              type: 'success',
              message: '修改成功！'
            })
          } else {
            await add(this.formData)
            this.$message({
              type: 'success',
              message: '添加成功！'
            })
          }
          this.$refs.formName.resetFields()
          this.formData = {
            subjectName: '',
            isFrontDisplay: ''
          }
          this.getSubjectlist()
          this.showDialog = false
        }
      })
    },
    // 删除
    delSubject (row) {
      this.$confirm('此操作将永久删除改文件,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row)
        this.$message({
          type: 'success',
          message: '删除成功！'
        })
        this.getSubjectlist()
      }).catch(() => {

      })
    },
    // 枚举
    formState (value) {
      const map = {
        1: '是',
        0: '否'
      }
      return map[value]
    },
    // 编辑对象
    async editSubject (subject) {
      this.dialogTitle = '修改学科'
      // const data = { id: subject.id, subjectName: subject.subjectName }
      //
      // console.log('---------' + res)
      this.formData = subject
      this.showDialog = true
    },
    goDetail (row) {
      this.$router.push({ path: 'directorys', query: row })
    },
    // 编辑对象
    editDetail (row) {
      this.$router.push({ path: '/subjects/tags', query: row })
    }
  }
}
</script>

<style scoped>
.search {
  font: blod, 10px, "幼圆";
  display: flex;
}
.container .total {
  margin-top: 18px;
  height: 35px;
  background-color: #f4f4f5;
  display: flex;
  justify-content: start;
  align-items: center;
  color: #909399;
  border-radius: 4px;
  font-size: 14px;
  margin-bottom: 20px;
}
.container.total i {
  margin-left: 15px;
  margin-right: 10px;
}
</style>
